/* GUI CONVERTED from rssoall.p (converter v1.75) Sat Aug 12 23:16:42 2000 */
/* rssoall.p - DETAIL SALES ORDER ALLOCATIONS BTB                          */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                     */
/* All rights reserved worldwide.  This is an unpublished work.            */
/*F0PN*/ /*V8:ConvertMode=Maintenance                                      */
/*K1Q4*/ /*V8:RunMode=Character,Windows                                    */
/* REVISION: 8.6     LAST MODIFIED: 06/14/96   BY: *K004* Kurt De Wit      */
/* REVISION: 8.6     LAST MODIFIED: 11/20/96   BY: *K022* Kieu Nguyen      */
/* REVISION: 8.6     LAST MODIFIED: 07/12/97   BY: *K0DH* Kieu Nguyen      */
/* REVISION: 8.6     LAST MODIFIED: 10/09/97   BY: *K0NV* Kieu Nguyen      */
/* REVISION: 8.6E    LAST MODIFIED: 05/20/98   BY: *K1Q4* Alfred Tan       */
/* REVISION: 8.6E    LAST MODIFIED: 06/01/98   BY: *K1NF* Niranjan R.      */
/* REVISION: 8.6E    LAST MODIFIED: 10/04/98   BY: *J314* Alfred Tan       */
/* REVISION: 9.0     LAST MODIFIED: 03/29/99   BY: *J3BM* Reetu Kapoor     */
/* REVISION: 9.0     LAST MODIFIED: 01/11/00   BY: *J3N7* Reetu Kapoor     */
/* REVISION: 9.1     LAST MODIFIED: 08/12/00   BY: *N0KP* myb              */

/*J3BM*/ /** TO IMPROVE PERFORMANCE   **/


          /* DEFINE GLOBAL VARIABLES */
          {mfdeclre.i}

           /* THE INPUT PARAMETERS CORRESPOND WITH THE VALUES OF THE       */
           /* abs_mstr RECORD USED.                                        */
           define input parameter shipper_id  like abs_id.
           define input parameter supplier_id like abs_shipfrom.

/*J3BM**   define new shared workfile work_abs_mstr like abs_mstr.  */

           define variable old_qty_all  like sod_qty_all.
           define variable this_lot     like ld_lot.
           define variable all_this_loc like sod_qty_all.
           define variable qty_to_all   like sod_qty_all.
/*K022*/   define variable parent_id  like abs_mstr.abs_id no-undo.
/*K022*/   define variable sod_recno  as recid no-undo.
/*K022*/   define variable err_flg  as logical no-undo.

           define buffer lddet          for ld_det.

/*J3BM*/   {rcwabsdf.i new}

           /* RETRIEVE THE abs_mstr RECORD USED. */
/*J3BM** BEGIN DELETE **
 *         find abs_mstr where abs_shipfrom = supplier_id
 *                         and abs_id       = shipper_id
 *         no-lock no-error.
 *J3BM** END DELETE **/

/*J3BM*/   for first abs_mstr
/*J3BM*/      fields (abs_id abs_line abs_order abs_shipfrom)
/*J3BM*/      where abs_shipfrom = supplier_id
/*J3BM*/      and   abs_id       = shipper_id no-lock:
/*J3BM*/   end. /* FOR FIRST ABS_MSTR */

           for each work_abs_mstr exclusive-lock:
              delete work_abs_mstr.
           end.

           /* EXPLODE SHIPPER TO GET ORDER DETAIL */
           {gprun.i ""rcsoisa.p"" "(input recid(abs_mstr))"}
/*GUI*/ if global-beam-me-up then undo, leave.


           processing-loop:
/*K022*           for each work_abs_mstr no-lock                */
/*K022*/   for each work_abs_mstr exclusive-lock
                     where work_abs_mstr.abs_qty <> work_abs_mstr.abs_ship_qty,
/*J3BM**       each abs_mstr no-lock  */
/*J3BM*/       each abs_mstr
/*J3BM*/          fields (abs_id abs_line abs_order abs_shipfrom) no-lock
                  where abs_mstr.abs_shipfrom = work_abs_mstr.abs_shipfrom
                    and abs_mstr.abs_id       = work_abs_mstr.abs_id,
/*J3BM**       first po_mstr no-lock */
/*J3BM*/       first po_mstr
/*J3BM*/           fields (po_is_btb po_nbr po_so_nbr) no-lock
                   where po_nbr = abs_order
                     and po_is_btb,
/*J3BM**       first pod_det no-lock */
/*J3BM*/       first pod_det
/*J3BM*/           fields (pod_line pod_nbr pod_part pod_qty_ord pod_sod_line)
/*J3BM*/           no-lock
                   where pod_nbr  = abs_order
                     and pod_line = integer(abs_line)
           break by abs_order by abs_line :

              /* ONLY ALLOCATIONS FOR BTB PO'S WITH POSITIVE QTYS */
              if pod_qty_ord <= 0 then next.

/*K022*      SKIP KIT COMPONENTS   */
/*K022*/     if work_abs_mstr.abs_item <> pod_part then next.

/*K0NV*      IN ML-EMT THE SBU ALSO ALLOCATES DIR-SHIP ORDER */
/*J3BM** BEGIN DELETE **
 *            find so_mstr where so_nbr     = po_so_nbr
 * /*K0DH*                      and so_primary = yes          */
 * /*K0NV* /*K0DH*/             and not so_secondary          */
 * /*K0NV*/                     and so_primary
 *            no-lock no-error.
 *J3BM** END DELETE **/

/*J3BM*/      for first so_mstr
/*J3BM*/         fields (so_nbr so_primary)
/*J3BM*/         where so_nbr = po_so_nbr
/*J3BM*/         and   so_primary no-lock:
/*J3BM*/      end. /* FOR FIRST SO_MSTR */

              if not available so_mstr then do:
                 {mfmsg.i 609 3}  /* Sales Order does not exist. */
                 next.
              end.

              find sod_det where sod_nbr  = po_so_nbr
                             and sod_line = pod_sod_line
              exclusive-lock no-error.
              if not available sod_det then do:
                 {mfmsg.i 764 3}  /* Sales Order line does not exist. */
                 next.
              end.

              if not sod_confirm then do:
                 {mfmsg.i 646 3}  /* Sales Order line not confirmed. */
                 next.
              end.

              if sod_sched then do:
                 {mfmsg.i 6014 3} /* Schedules exists for SO line. */
                 next.
              end.

              /* CHECK FOR SITE SECURITY */
              {gprun.i ""gpsiver.p"" "(input  sod_site,
                                       input  ?,
                                       output return_int)"}
/*GUI*/ if global-beam-me-up then undo, leave.

              if return_int = 0 then do:
                 {mfmsg.i 725 3} /* USER DOES NOT HAVE ACCESS TO THIS SITE. */
                 next.
              end.

              assign old_qty_all = sod_qty_all
/*K022*/      parent_id = abs_id
/*K022*/      sod_recno = recid(sod_det).

/*K022*        ALLOCATE REGULAR ITEMS */
/*K022*/      if sod_cfg_type <> "2"
/*K022*/      or (sod_cfg_type = "2" and sod_fa_nbr <> "") then do:

              if sod_type = "" then do:

                 assign this_lot = ?.

                 for each lad_det where lad_dataset = "sod_det"
                                    and lad_nbr     =  sod_nbr
                                    and lad_line    =  string(sod_line)
                                    and lad_part    =  sod_part
                 no-lock:
                    accum lad_qty_all (total).
                    if this_lot = ? and lad_qty_all > 0 then this_lot = lad_lot.
                 end.

                 assign sod_qty_all = (accum total lad_qty_all) / sod_um_conv.
/*J3N7**         assign qty_to_all  = min(sod_qty_ord - sod_qty_pick -  */
/*J3N7**                                  sod_qty_ship - sod_qty_all,   */
/*J3N7**                                  work_abs_mstr.abs_qty).       */

/*J3N7*/         assign
/*J3N7*/            qty_to_all = min((sod_qty_ord - sod_qty_pick - sod_qty_ship
/*J3N7*/                              - sod_qty_all) * sod_um_conv,
/*J3N7*/                              work_abs_mstr.abs_qty).

                 if qty_to_all > 0 then do:
/*J3BM**            find pt_mstr where pt_part = sod_part no-lock no-error. */
/*J3BM*/            for first pt_mstr
/*J3BM*/               fields (pt_part pt_sngl_lot)
/*J3BM*/               where pt_part = sod_part no-lock:
/*J3BM*/            end. /* FOR FIRST PT_MSTR */

                    if pt_sngl_lot = no then this_lot = ?.

                    ld_det_loop:
/*J3N7*/            /* UPDATING THE LD_DET FOR THE REQUIRED LOT/REF */
                    for each ld_det where  ld_site = sod_site
/*J3BM**                              and  ld_part = sod_part              */
                                      and  ld_loc  = work_abs_mstr.abs_loc
/*J3BM*/                              and  ld_part = sod_part
/*J3N7*/                              and  ld_lot  = work_abs_mstr.abs_lotser
/*J3N7*/                              and  ld_ref  = work_abs_mstr.abs_ref
                                      and  can-find(is_mstr
                                                    where is_status = ld_status
                                                     and is_avail  = yes      )
                                      and  ld_qty_oh - ld_qty_all > 0
                                      and (ld_expire > today or ld_expire = ?)
                    exclusive-lock:

                       if this_lot <> ? and ld_lot <> this_lot
                       then next ld_det_loop.

                       if   qty_to_all   < ld_qty_oh - ld_qty_all
                       then all_this_loc = qty_to_all.
                       else all_this_loc = ld_qty_oh - ld_qty_all.

                       if pt_sngl_lot and
                          all_this_loc < qty_to_all and this_lot = ?
                       then do for lddet:
/*J3BM** BEGIN DELETE **
 *                        for each lddet where lddet.ld_site = sod_site
 *                                         and lddet.ld_part = sod_part
 *                                         and lddet.ld_loc  =
 *                                                        work_abs_mstr.abs_loc
 *J3BM** END DELETE **/
/*J3BM*/                  for each lddet
/*J3BM*/                     where lddet.ld_part = sod_part
                             and   lddet.ld_lot  = ld_det.ld_lot
                             and   lddet.ld_ref  = ld_det.ld_ref
/*J3BM*/                     and   lddet.ld_site = sod_site
/*J3BM*/                     and   lddet.ld_loc  = work_abs_mstr.abs_loc
                             and can-find(is_mstr
                                           where is_status = lddet.ld_status
                                           and is_avail)
                             and (lddet.ld_expire > today or
                                  lddet.ld_expire = ?)
                             and (lddet.ld_qty_oh -
                                         lddet.ld_qty_all ) >= qty_to_all
                             no-lock:
                             accum (lddet.ld_qty_oh - lddet.ld_qty_all) (total).
                          end.

                          if (accum total lddet.ld_qty_oh - lddet.ld_qty_all) >=
                              qty_to_all
                          then this_lot = ld_det.ld_lot.

                       end. /* do for ld_det */

                       /* IF ALL AVAILABLE TO ALLOCATE OR NOT SINGLE LOT THEN */
                       /* CREATE LAD_DET                                      */
                       if all_this_loc = qty_to_all or pt_sngl_lot = no or
                          (this_lot <> ? and ld_lot = this_lot)
                       then do:

                          find lad_det where lad_dataset = "sod_det"
                                         and lad_nbr     =  sod_nbr
                                         and lad_line    =  string(sod_line)
                                         and lad_part    =  sod_part
                                         and lad_site    =  sod_site
                                         and lad_loc     =
                                                          work_abs_mstr.abs_loc
                                         and lad_lot     =
                                                       work_abs_mstr.abs_lotser
                                         and lad_ref     =
                                                          work_abs_mstr.abs_ref
                          exclusive-lock no-error.
                          if not available lad_det then do:

                             create lad_det.
                             assign lad_dataset = "sod_det"
                                    lad_nbr     =  sod_nbr
                                    lad_line    =  string(sod_line)
                                    lad_site    =  sod_site
                                    lad_part    =  sod_part
                                    lad_loc     =  work_abs_mstr.abs_loc
                                    lad_lot     =  work_abs_mstr.abs_lotser
                                    lad_ref     =  work_abs_mstr.abs_ref.
                          end.

                          assign qty_to_all  = qty_to_all  - all_this_loc
                                 ld_qty_all  = ld_qty_all  + all_this_loc
                                 lad_qty_all = lad_qty_all + all_this_loc.

                       end. /* all_this_loc = qty_to_all or pt_single_lot */

                       if qty_to_all <= 0 then leave.

                    end. /* ld_det_loop */

                 end. /* qty_to_all > 0 */

              end.  /* sod_type = "" */

              for each lad_det where lad_dataset = "sod_det"
                                 and lad_nbr     = sod_nbr
                                 and lad_line    = string(sod_line)
              no-lock:
                 accum lad_qty_all (total).
              end.

              assign sod_qty_all = (accum total lad_qty_all) / sod_um_conv.

              find in_mstr where in_site = sod_site
                             and in_part = sod_part
              exclusive-lock no-error.
              if available in_mstr
              then assign in_qty_all = in_qty_all + (accum total lad_qty_all) -
                                       (old_qty_all * sod_um_conv).

/*K022*/   end.    /* sod_cfg_type <> "2" */
/*K022*/   else do:  /* ALLOCATE KIT COMPONENTS */
/*K022*/      err_flg = no.
/*K022*/      {gprun.i ""rssoalla.p"" "(input parent_id,
                    input supplier_id,
                    input sod_recno,
                    output err_flg)"}
/*GUI*/ if global-beam-me-up then undo, leave.


/*K022*        IF CAN NOT ALLOCATE ALL COMPONENT THEN MARK THIS REC     */
/*K022*        SO LATER WILL NOT CREATE SO SHIPPER FOR THIS ITEM        */
/*K1NF** /*K022*/ if err_flg then work_abs_mstr.abs__qad10 = "X".       */
/*K1NF*/       if err_flg then substring(work_abs_mstr.abs__qad10,1,1) = "X".

/*K022*/   end.

           end. /* processing-loop */
